package com.example.sale.controller;

import com.example.customer.service.CusInfoService;
import com.example.sale.entity.SaleChance;
import com.example.sale.service.SaleService;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/")
public class SaleController {

    @Resource(name = "SaleServiceImpl")
    private SaleService saleService;

    @RequestMapping("/queryChance")
    public String dataDictionary(@ModelAttribute SaleChance saleChance,
               @RequestParam(value="pageNum",defaultValue="1")int pageNum,
               @RequestParam(value="pageSize",defaultValue="5")int pageSize,
                    HttpServletRequest request){

        PageInfo<SaleChance> pageInfo =  saleService.queryData(saleChance,pageNum,pageSize);
        request.setAttribute("pageInfo",pageInfo);
        return "sales/sale/saleCM";
    }

    @RequestMapping("/deleteInfo")
    public String deleteData(SaleChance saleChance, HttpServletRequest request){

        String id = saleChance.getId();
        saleService.deleteInfo(id);
        return "redirect:/queryChance";
    }

    //条件查询
    /* 编辑数据前首先要根据id 查询出当前数据 */
    //时间为CST类型,需转换
    @RequestMapping("/queryInfo")
    public String queryInfo(SaleChance saleChance,
            @RequestParam(value="pageNum",defaultValue="1")int pageNum,
            @RequestParam(value="pageSize",defaultValue="5")int pageSize,
                            HttpServletRequest request){

        String id = saleChance.getId();
        PageInfo<SaleChance> pageInfo = saleService.queryData(saleChance,pageNum,pageSize);
        request.setAttribute("pageInfo",pageInfo);

        return "sales/sale/compile";
    }

    //修改数据
    @RequestMapping("/updateInfo")
    public String updateData(SaleChance saleChance, HttpServletRequest request){

        String id = saleChance.getId();
        saleService.updateInfo(saleChance);
        request.setAttribute("msg","修改成功!");
        return "sales/sale/compile";
    }

    @RequestMapping("/saveInfo")
    public String saveData(SaleChance saleChance, HttpServletRequest request){

       if(StringUtils.isEmpty(saleChance.getCreator())){
           String operator  = (String) request.getSession().getAttribute("user");
           saleChance.setCreator(operator);
       }
       if(!StringUtils.isEmpty(saleChance)){
           saleService.saveInfo(saleChance,request);
           request.setAttribute("msg","添加成功!");
       }
        return "sales/sale/createSaleChance";
    }

    //客户开发机会
    @RequestMapping("/queryOppo")
    public String queryOppo(@ModelAttribute SaleChance saleChance,
           @RequestParam(value="pageNum",defaultValue="1")int pageNum,
           @RequestParam(value="pageSize",defaultValue="5")int pageSize,
           HttpServletRequest request){

        PageInfo<SaleChance> pageInfo = saleService.queryData(saleChance,pageNum,pageSize);
        request.setAttribute("pageInfo",pageInfo);

        return "sales/plan/develop";
    }

    @Resource(name = "CusInfoServiceImpl")
    private CusInfoService cusInfoService;

    //客户开发状态修改为成功
    @RequestMapping("/success")
    public String success(@ModelAttribute SaleChance saleChance,
                            @RequestParam(value="pageNum",defaultValue="1")int pageNum,
                            @RequestParam(value="pageSize",defaultValue="5")int pageSize,
                            HttpServletRequest request,String cus_id,String status){

        String id = request.getParameter("cus_id");
        String info = request.getParameter("status");

        cusInfoService.updStatus( id, info);


        return "redirect:queryOppo";
    }

}
